<?php
/**
 * @filename NetworkDomain.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2014-10-22 15:09:17
 * @version 1.0
 * @Description 网点银行
 */
class NetworkBank extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'network_bank';
    }
    /**
     * 根据域名查找
     */
    public function findByDomain($domain){
        return $this->find('domain=:domain', array(':domain' => $domain));
    }
    /**
     * 根据银行名称查找
     */
    public function findByBank($bank){
        return $this->find('bank_name=:bank_name',array(':bank_name' => $bank));
    }

    /**
     * 获取key=》val模式的列表
     */
    public function getKv(){
        $data = $this->findAll();
        $result = array();
        foreach ($data as $key => $val) {
            $result[$val['bank_id']] = $val['bank_name'];
        }
        return $result;
    }
	
	/**
	 * 添加银行，返回银行id
	 * @param type $bankName
	 * @param type $bankDomain
	 * @return int
	 */
	public function addNetworkBank($bankName, $bankDomain = null) {
		if(!$bankName){
			return 0;
		}
		$bank = $this->find('bank_name=:name', array(':name' => $bankName));
		if($bank){
			return $bank->bank_id;
		}
		$networkBankModel = new NetworkBank();
		$networkBankModel->bank_name = $bankName;
		$networkBankModel->domain = $bankDomain;
		$networkBankModel->save();
		return $networkBankModel->attributes['bank_id'];
	}
}
